[\D\S] vs. [^\d\s]

Hello,

I'm reading this excellent tutorial: http://www.regular-expressions.info/charclass.html
However in the "Negated Shorthand Character Classes" section, I didn't
understand this:

"Be careful when using the negated shorthands inside square brackets.
[\D\S] is not the same as [^\d\s]. The latter will match any character
that is not a digit or whitespace. So it will match x, but not 8. The
former, however, will match any character that is either not a digit,
or is not whitespace. Because a digit is not whitespace, and
whitespace is not a digit, [\D\S] will match any character, digit,
whitespace or otherwise".

Doesn't it look like the same thing? Please let me know!
Thanks,

--
Charles.
landemaine [ Do, 29 November 2007 20:48 ] [ ID #1881358 ]

Re: [\D\S] vs. [^\d\s]

On 11/29/2007 1:48 PM, Charles A. Landemaine wrote:
> Hello,
>
> I'm reading this excellent tutorial: http://www.regular-expressions.info/charclass.html
> However in the "Negated Shorthand Character Classes" section, I didn't
> understand this:
>
> "Be careful when using the negated shorthands inside square brackets.
> [\D\S] is not the same as [^\d\s]. The latter will match any character
> that is not a digit or whitespace. So it will match x, but not 8. The
> former, however, will match any character that is either not a digit,
> or is not whitespace. Because a digit is not whitespace, and
> whitespace is not a digit, [\D\S] will match any character, digit,
> whitespace or otherwise".
>
> Doesn't it look like the same thing? Please let me know!

No, it doesn't. It's the same as the difference between:

"(not 7) OR (not 3)" versus "not (7 OR 3)"

By applying boolean algebra the former is equivalent to "not (7 AND 3)" which of
course is any number since no number is both 7 and 3.

In your case, "[\D\S]" means "(not digit) OR (not space)" is equivalent to "not
(digit AND space)" which is any character since no character is both a digit and
a space, whereas "[^\d\s]" means "not (digit OR space)" which is only the
characters that are neither a digit nor a space (e.g. an alphabetic character or
a punctioanion mark, or...).

Ed.
Ed Morton [ Do, 29 November 2007 22:19 ] [ ID #1881364 ]

Re: vs. [^\d\s]

On Nov 29, 6:19 pm, Ed Morton <mor... [at] lsupcaemnt.com> wrote:
> No, it doesn't. It's the same as the difference between:
>
> "(not 7) OR (not 3)" versus "not (7 OR 3)"
>
> By applying boolean algebra the former is equivalent to "not (7 AND 3)" which of
> course is any number since no number is both 7 and 3.
>
> In your case, "[\D\S]" means "(not digit) OR (not space)" is equivalent to "not
> (digit AND space)" which is any character since no character is both a digit and
> a space, whereas "[^\d\s]" means "not (digit OR space)" which is only the
> characters that are neither a digit nor a space (e.g. an alphabetic character or
> a punctioanion mark, or...).
>


Thanks Ed for the explanatin ;)

--
Charles.
landemaine [ Mo, 03 Dezember 2007 12:12 ] [ ID #1884289 ]
Linux » comp.unix.shell » [\D\S] vs. [^\d\s]

Vorheriges Thema: Removing numbers
Nächstes Thema: input of pwd and ls -l in a file without using >>